package ovm.gui.forms;

import ovm.entity.EntityFactoryEntity;
import ovm.management.administration.factory.EntityFactoryManager;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Created by IntelliJ IDEA.
 * User: Jokser
 * Date: 28.05.12
 * Time: 8:57
 * To change this template use File | Settings | File Templates.
 */

public class EntityFormFactory {

    private static final String packagePath = "ovm.gui.forms.";
    
    public static AbstractEntityForm createEntityForm (Class entityClass, Object entity, Boolean editable) {
        // get entity class name
        String entityClassName = entityClass.getSimpleName();

        EntityFactoryEntity properties = EntityFactoryManager.getFactoryProperties(entityClassName);
        if (properties == null) return null;

        String formClassName = packagePath + properties.getEntityForm();

        Constructor constructor = null;
        try {
            constructor = Class.forName(formClassName).getConstructor(Object.class, Boolean.class);
            return (AbstractEntityForm) constructor.newInstance(entity, editable);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (ClassNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (InvocationTargetException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (InstantiationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IllegalAccessException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        return null;
    }
    
}
